package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Admin;
import com.geyue.core.model.Top;
import com.geyue.core.service.LiveService;
import com.geyue.core.service.TeacherService;
import com.geyue.core.service.TopService;
import com.geyue.core.service.VedioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/6/6.
 */
@Controller
@RequestMapping("/top")
public class TopController {

    @Autowired
    private LiveService liveService;
    @Autowired
    private VedioService vedioService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private TopService topService;


    @RequestMapping(value = "/live", method = RequestMethod.GET)
    public String live(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        model.addAttribute("data",liveService.getTopList());
        return "top/live_list";
    }

    @RequestMapping(value = "/vedio", method = RequestMethod.GET)
    public String vedio(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        model.addAttribute("data",vedioService.getTopList());
        return "top/vedio_list";
    }

    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    public String teacher(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        model.addAttribute("data",teacherService.getTopList());
        return "top/teacher_list";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Res delete(@PathVariable Integer id, Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过期,请重新登录!");

        if(topService.deleteByKey(id) > 0)
            return  Res.success("操作成功!");

        return Res.fail("操作失败,请重试!");
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res delete(Top top, Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过期,请重新登录!");

        if(topService.saveNotEmpty(top) > 0)
            return  Res.success("操作成功!");

        return Res.fail("操作失败,请重试!");
    }

}
